const express = require('express');
const pool = require('../utils/pool')
let router = express.Router();
router.get('/allSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from car where userId = ?',[obj.userId],(x,y)=>{
		if(x){
			throw x
		}
		// console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
router.post('/carAdd',(req,res)=>{
  let obj = req.body
  console.log(obj);
	pool.query('insert into car set ?',[obj],(x,y)=>{
		if(x){
			throw x;
		}
		console.log(y);
		res.send({code:200,msg:'添加成功'})
	})
 
});
router.get('/carDelete',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('delete from car where id = ?',[obj.id],(x,y)=>{
		if(x){
			throw x
		}
		console.log(y);
		if(y.affectedRows===0){
			res.send({code:400,msg:'该商品不存在'})
		}else{
			res.send({code:200,msg:'删除成功'})
		}
		
	})
	
})
router.get('/idSearch',(req,res)=>{
	var obj= req.query
	console.log(obj);
	pool.query('select*from car where id = ?',[obj.id],(x,y)=>{
		if(x){
			throw x
		}
		console.log(x);
    console.log(y);
		if(y.length===0){
			res.send({code:400,msg:'查询错误'})
		}else{
			res.send({code:200,msg:'查询成功' ,data:y})
		}
		
	})
	
})
module.exports=router;